package org.hxd.config;
import org.hxd.pojo.User_inf;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class loginFilter implements Filter{
    String [] IG_URI={"/index.jsp","loginFrom.jsp","/login","/",".css",".js",".jpg"};
    @Override
    public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest)servletrequest;

        HttpServletResponse response = (HttpServletResponse)servletresponse;
        /*获取本次请求的URI*/
        String requestURI = request.getRequestURI();

        for(String s: IG_URI){
            if(requestURI.endsWith(s)){
                chain.doFilter(request,response);
                return ;
            }
        }
        User_inf user_inf = (User_inf) request.getSession().getAttribute("user");
        if(user_inf!=null){
            chain.doFilter(request,response);
        }else{
            request.setAttribute("msg","请登陆访问后");
            request.getRequestDispatcher("/index.jsp").forward(request,response);
        }
    }
    @Override
    public void destroy() {
    }
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
}
